package com.tender.utils;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * 工具类，用于屏蔽掉 RestHighLevelClient 客户端的创建
 */
/*
// 使用 demo
ESOperationTemplate.process(client -> {

            try {

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return null;
        });
 */
public class ESOperationTemplate {

    public static void process(TaskConsumer consumer) throws Exception {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        try {
            // 具体任务的处理
            consumer.process(client);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            client.close();
        }
    }

}
